package com.xtt.dynamic.service.impl;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.xtt.dynamic.entity.Friend;
import com.xtt.dynamic.mapper.FriendMapper;
import com.xtt.dynamic.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FriendServiceImpl implements FriendService {


    @Autowired
    private FriendMapper friendMapper;



    //读--读库
    @Override
    @DS("slave_1")  //从库，如果按照下划线方式配置了多个，可以指定前缀即可(组名)
    public List<Friend> list() {
        return friendMapper.list();
    }

    //写-写库
    @Override
    @DS("master")
    public void save(Friend friend) {
        friendMapper.save(friend);
    }

    @Override
    @DS("master")
    public void saveW(Friend friend) {
        friendMapper.save(friend);
    }

    @Override
    @DS("slave_1")
    public void saveR(Friend friend) {
        friendMapper.save(friend);
        int a=1/0;
    }

    @Override
    @DSTransactional
    public void saveAll(Friend friend) {
        //指定多数据源操作
        saveW(friend);
        saveR(friend);
    }

}
